package org.wisteria.framework.core.configuration;

import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.wisteria.framework.core.app.ModuleConstants;

/**
 * 
 * @author HuaShengPi
 *
 */
@Configuration
@AutoConfigureAfter(MybatisConfiguration.class)
public class MybatisConfiguration implements EnvironmentAware {
	
	private static final Logger logger = LoggerFactory.getLogger(MybatisConfiguration.class);

    //private String mybatisMapperAnnotation;
	
	private String modulesDaoPackages;
    
	@Override
	public void setEnvironment(Environment environment) {
        //mybatisMapperAnnotation = environment.resolvePlaceholders("${wisteria.core.mybatis.mapper.scan.annotation:}");
        modulesDaoPackages = environment.resolvePlaceholders("${"+ ModuleConstants.APP_MODULES_DAO_PACKAGES + ":}");
        logger.info("wisteria load "  + ModuleConstants.APP_MODULES_DAO_PACKAGES + ":" + modulesDaoPackages);
	}
	
	@Bean
    public MapperScannerConfigurer mapperScannerConfigurer() {
		MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
        mapperScannerConfigurer.setBasePackage(modulesDaoPackages);
        return mapperScannerConfigurer;
    }

	@Bean
	public MybatisDevInterceptor mybatisDevInterceptor() {
		MybatisDevInterceptor mybatisDevInterceptor = new MybatisDevInterceptor();
		mybatisDevInterceptor.setEnabled(true);
		return mybatisDevInterceptor;
	}

}
